package com.book.lizhi.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

import com.book.lizhi.BaseActivity;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.util.Util;

public class UserInfoActivity extends BaseActivity {
	private CheckBox mAutoCheckBox;
	
	private LinearLayout shareAppView;
	private LinearLayout feedbackView;
	private LinearLayout getPointsView;
	private LinearLayout aboutView;

	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_info);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toobar);
		setSupportActionBar(toolbar);
		initUI();
	}

	private void initUI() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setTitle("设置");

		mAutoCheckBox = (CheckBox) findViewById(R.id.auto_get_checkbox);
		boolean auto = Util.getAutoGetSharePreference(Constants.AUTO_GET);
		mAutoCheckBox.setChecked(auto);
		
		shareAppView = (LinearLayout)findViewById( R.id.share_app_view );
		feedbackView = (LinearLayout)findViewById( R.id.feedback_view );
		getPointsView = (LinearLayout)findViewById( R.id.get_points_view );
		aboutView = (LinearLayout)findViewById( R.id.about_view );

		addOnListener();
	}
	
	

	private void addOnListener() {
		shareAppView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent();
				sendIntent.setAction(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_TEXT,"http://www.wandoujia.com/apps/com.book.lizhi");
				sendIntent.setType("text/plain");
				startActivity(Intent.createChooser(sendIntent, "励志应用分享"));
			}
		});
		feedbackView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent data=new Intent(android.content.Intent.ACTION_SEND); 
				String[] email={"1312742798@qq.com"};
				data.putExtra(android.content.Intent.EXTRA_EMAIL, email);
				data.putExtra(Intent.EXTRA_SUBJECT, "意见反馈"); 
				data.putExtra(Intent.EXTRA_TEXT, "我对应用的意见如下:\n"); 
				data.setType("message/rfc822");
				startActivity(Intent.createChooser(data, "请选择邮件发送软件"));
			}
		});
		getPointsView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(), AdvActivity.class));
			}
		});

		aboutView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(), AboutActivity.class));
			}
		});
		mAutoCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Util.saveDataBySharePreference(Constants.AUTO_GET, isChecked);
			}
		});
		
		findViewById(R.id.give_score_view).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("market://details?id=" + getPackageName());
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
